package servlet;

import dao.GoodsDao;
import dao.OrderDao;
import dao.TypeDao;
import entity.Goods;
import entity.Order;
import entity.Type;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/orderList")
public class OrderPageDataServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		final int SIZE = 8;
		System.out.println("------------orderList-------------");
		String orderId = request.getParameter("selectOrderID");
		String goodsId = request.getParameter("selectGoodsID");
		String userId = request.getParameter("selectUserID");
		String goodsName = request.getParameter("selectGoodsName");
		String state = request.getParameter("selectOrderState");
		StringBuilder condition = new StringBuilder();
		StringBuilder urlParams =new StringBuilder();
		ArrayList<Object> parmas = new ArrayList<>();
		condition.append("1=1");
		if (orderId != null && !orderId.isEmpty()){
			condition.append(" and order_id like ?");
			parmas.add("%" + orderId + "%");
			urlParams.append("&selectOrderID=" + URLEncoder.encode(orderId,"utf-8"));
		}
		if(goodsId != null && !goodsId.isEmpty()){
			condition.append(" and goods_id like ?");
			parmas.add("%" + goodsId + "%");
			urlParams.append("&selectGoodsID=" + URLEncoder.encode(goodsId,"utf-8"));
		}
		if (userId != null && !userId.isEmpty()){
			condition.append(" and user_id like ?");
			parmas.add("%" + userId + "%");
			urlParams.append("&selectUserID=" + URLEncoder.encode(userId,"utf-8"));
		}
		if (goodsName != null && !goodsName.isEmpty()){
			condition.append(" and goods_name like ?");
			parmas.add("%" + goodsName + "%");
			urlParams.append("&selectGoodsName=" + URLEncoder.encode(goodsName,"utf-8"));
		}
		if (state != null && !state.isEmpty()){
			condition.append(" and state = ?");
			parmas.add(state);
			urlParams.append("&selectOrderState=" + URLEncoder.encode(state,"utf-8"));
		}
		Integer page = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			page = 1;
		}
		int start = (page - 1)*SIZE;
		OrderDao dao = new OrderDao();
		List<Order> list = dao.queryOrderPage(start, SIZE, condition.toString(), parmas);
		int total = dao.total_orders(condition.toString(),parmas);
		int pageNum = total / SIZE;
		if (total % SIZE != 0) {
			pageNum += 1;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= pageNum; i++) {
			String str = "<a href=\"#url\">第" + i + "页</a>&nbsp;&nbsp;";
			str = str.replace("#url", request.getContextPath() + "/orderList?page=" + i + urlParams.toString());
			System.out.println("/orderList?page=" + i + urlParams.toString());
			if (page == i) {
				str = "第" + i + "页&nbsp;&nbsp;";
			}
			sb.append(str);
		}
		System.out.println(sb);
		request.setAttribute("page", sb.toString());
		request.setAttribute("orders", list);
		request.getRequestDispatcher("./backend/order_list.jsp").forward(request, response);
	}


}
